﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Collections;
using ZedGraph;
using EasyStudy.Logic.DataStatistics;
using BarChart;

namespace EasyStudy.UI
{
    public partial class FrmDataStatistics : KryptonForm
    {
        private string[] zhMonth = {
           "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"                         
        };

        private Color[] colors = {
            Color.FromArgb(255, 200, 255, 255), 
            Color.FromArgb(255, 150, 200, 255), 
            Color.FromArgb(255, 100, 100, 200), 
            Color.FromArgb(255, 255, 60, 130), 
            Color.FromArgb(255, 250, 200, 255), 
            Color.FromArgb(255, 255, 255, 0), 
            Color.FromArgb(255, 255, 155, 55), 
            Color.FromArgb(255, 150, 200, 155), 
            Color.FromArgb(255, 255, 255, 200), 
            Color.FromArgb(255, 100, 150, 200), 
            Color.FromArgb(255, 130, 235, 250), 
            Color.FromArgb(255, 150, 240, 80)
        };

        public enum YearMonth { 
            Year,
            Month
        }

        public enum GraphType{
            Line,
            Bar
        }

        private int year;
        private int month;

        private BarItem bi;
        private LineItem li;

        public FrmDataStatistics()
        {
            InitializeComponent();
            initControls();
        }

        private void drawCurrentMonth()
        {
            year = DateTime.Now.Year;
            month = DateTime.Now.Month;
            int[] nums = DataStatisticsDBMgr.GetMonthTaskNumbers(year, month);
            GraphType gt = GraphType.Bar;

            if (rbBar.Checked)
            {
                gt = GraphType.Bar;
            }
            else
            {
                gt = GraphType.Line;
            }
            drawData(YearMonth.Month, nums, gt);
        }

        private void draw()
        {
            year = (int)txtYear.Value;
            if (cmbMonths.SelectedItem == null) 
            {
                cmbMonths.SelectedIndex = 0;
            }
            month = int.Parse(cmbMonths.SelectedItem.ToString());
            int[] nums = DataStatisticsDBMgr.GetMonthTaskNumbers(year, month);
            GraphType gt = GraphType.Bar;
            
            if(rbBar.Checked)
            {
                gt = GraphType.Bar;
            }
            else
            {
                gt = GraphType.Line;
            }

            drawData(YearMonth.Month, nums, gt);
        }

        private void initControls()
        {
            txtYear.Value = DateTime.Now.Year;
            cmbMonths.SelectedIndex = DateTime.Now.Month - 1;
            rbBar.Checked = true;
            drawCurrentMonth();

            barChart.Description.Visible = true;
            barChart.Label.Visible = true;
            barChart.Border.Width = 10;
            barChart.Shadow.Mode = CShadowProperty.Modes.Both;
            barChart.Shadow.WidthInner = 1;
            barChart.Shadow.WidthOuter = 4;
            barChart.Shadow.ColorOuter = Color.FromArgb(100, 0, 0, 0);
        }

        private void drawData(YearMonth ym, int[] taskNumbers, GraphType gt)
        {
            GraphPane panel = zedGraphControl.GraphPane;

            int j = barChart.Count;
            while (j > 0)
            {
                barChart.RemoveAt(0);
                --j;
            }

            //设置图标标题和x、y轴标题
            if (ym == YearMonth.Year)
            {
                panel.Title.Text = year.ToString();
            }
            else
            {
                panel.Title.Text = year.ToString() + "/" + month.ToString();
                barChart.Description.Text = panel.Title.Text;
            }

            panel.XAxis.Title.Text = "时间";
            panel.YAxis.Title.Text = "任务数";
            panel.YAxis.IsVisible = true;
            panel.Legend.IsVisible = false;

            PointPairList list1 = new PointPairList();
            int xLen = 0;
            if (ym == YearMonth.Year)
            {
                xLen = 12;
            }
            else
            {
                // 获取这个月的天数
                xLen = DateTime.DaysInMonth(year, month);
            }

            for (int i = 0; i < xLen; ++i) {
                String tag = null;
                if (ym == YearMonth.Year)
                {
                    tag = zhMonth[i] + "月";
                }
                else
                {
                    tag = (i + 1).ToString() + "日";
                }
                list1.Add(i, taskNumbers[i], tag);
                barChart.Add(taskNumbers[i], (i + 1).ToString(), colors[i % colors.Length]);
            }

            if (gt == GraphType.Bar)
            {
                if (bi == null)
                {
                    bi = panel.AddBar("EbbTask", list1, Color.DarkGreen);
                }
                else
                {
                    bi.Points = list1;
                }
                if (li != null)
                {
                    li.Clear();
                }
                zedGraphControl.Visible = false;
                barChart.Visible = true;
            }
            else
            {
                zedGraphControl.Visible = true;
                barChart.Visible = false;
                if (li == null)
                {
                    li = panel.AddCurve("EbbTask", list1, Color.DarkBlue, SymbolType.None);
                    li.Line.Width = 3;
                }
                else
                {
                    li.Points = list1;
                }
                if (bi != null)
                {
                    bi.Clear();
                }
            }

            //填充图表颜色
            panel.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);

            //以上生成的图标X轴为数字，下面将转换为日期的文本
            string[] labels = null;
            if (ym == YearMonth.Year)
            {
                labels = new string[12];
                for (int i = 0; i < 12; ++i) {
                    labels[i] = zhMonth[i] + "月";
                }
            }
            else
            {
                labels = new string[DateTime.DaysInMonth(year, month)];
                for (int i = 0; i < labels.Length; ++i)
                {
                    labels[i] = month + "-" + (i + 1);
                }
            }

            panel.XAxis.Scale.TextLabels = labels;
            panel.XAxis.Type = AxisType.Text;
            panel.YAxis.Scale.MajorStep = 1;
            
            zedGraphControl.AxisChange();
            Refresh();
            barChart.RedrawChart();
        }

        private void rbLine_CheckedChanged(object sender, EventArgs e)
        {
            draw();
        }

        private void cmbMonths_SelectedIndexChanged(object sender, EventArgs e)
        {
            draw();
        }

        private void txtYear_ValueChanged(object sender, EventArgs e)
        {
            draw();
        }
    }
}
